import Vue from 'vue'
import Router from 'vue-router'
import store from './store'

Vue.use(Router);

let router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import('./views/Login')
        },
        {
            path: '/',
            name: 'index',
            component: () => import('./views/Index'),
            children: [{
                path: 'project',
                name: 'project',
                component: () => import('./views/project/AppList')
            },{
                path: 'project/:id',
                name: 'detail',
                component: () => import('./views/project/AppDetail'),
                children:[{
                    path: 'info',
                    name: 'info',
                    component: () => import('./views/project/detail/Info')
                },{
                    path: 'db',
                    name: 'db',
                    component: () => import('./views/project/detail/Db')
                },{
                    path: 'api',
                    name: 'api',
                    component: () => import('./views/project/detail/Api')
                },{
                    path: 'plugins',
                    name: 'plugins',
                    component: () => import('./views/project/detail/Plugins')
                },{
                    path: 'server',
                    name: 'server',
                    component: () => import('./views/project/detail/Server')
                },{
                    path: 'images',
                    name: 'images',
                    component: () => import('./views/project/detail/Images')
                },{
                    path: 'history/:image',
                    name: 'history',
                    component: () => import('./views/project/detail/History')
                }]
            },{
                path: 'create',
                component: () => import('./views/project/AppCreate')
            },{
                path: 'server',
                component: () => import('./views/server/Server')
            },{
                path: 'server/create',
                component: () => import('./views/server/ServerCreate')
            }]
        }
    ]
});

router.beforeEach((to, from, next) => {
    if (!store.getters.userInfo) {
        if (to.name === 'login') {
            store.dispatch('clearUserInfo');
            next();
        } else {
            store.dispatch('fetchUserInfo').then((userInfo)=> {
                if (!userInfo) {
                    router.replace({name: 'login'})
                }
                next();
            }).catch(() => {
                store.dispatch('clearUserInfo');
                router.replace({name: 'login'});
                next();
            })
        }
    } else if (store.getters.userInfo && to.name === 'login') {
        router.replace({name: 'index'});
        next();
    } else {
        next();
    }
});

export default router;
